package com.tajiang.business.businesscount.store;

import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import com.tajiang.business.R;
import com.tajiang.business.common.activity.BaseActivity;
import com.tajiang.business.common.utils.CommandTools;
import com.tajiang.business.common.utils.PostDataTools;
import com.tajiang.business.common.utils.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class StoreNoticeActivity extends BaseActivity {

    @BindView(R.id.store_notice_number)
    TextView tvNumber;

    @BindView(R.id.store_notice_content)
    EditText edtContent;

    private final int maxLength = 120;//字数限制
    private String strContent;

    @Override
    protected void initTopBar() {

    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_store_notice);
    }

    @Override
    protected void initData() {
        setTitle("档口公告");

        strContent = getIntent().getStringExtra("notice");

        if(TextUtils.isEmpty(strContent)){
            strContent = "";
        }

        edtContent.setText(strContent);
        edtContent.setSelection(strContent.length());
        tvNumber.setText(strContent.length() + "/" + maxLength);

        edtContent.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                String strMsg = s.toString();
                Log.v("zd", strMsg);
                if(strMsg.length() > maxLength){

                    mHandler.sendEmptyMessage(0x0022);
                    tvNumber.setText(maxLength + "/" + maxLength);
                }else{
                    tvNumber.setText(strMsg.length() + "/" + maxLength);
                }

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });
    }

    public Handler mHandler = new Handler(){

        public void handleMessage(Message msg){

            String str = edtContent.getText().toString();

            edtContent.setText(str.substring(0, maxLength));
            edtContent.setSelection(maxLength);

            tvNumber.setText(maxLength + "/" + maxLength);
            ToastUtils.showShort("输入字数达到上限");
        }
    };

    @OnClick(R.id.store_notice_submit)
    public void submit(){

        String notice = edtContent.getText().toString();
        if(TextUtils.isEmpty(notice)){
            ToastUtils.showShort("内容不能为空");
            return;
        }

        PostDataTools.shop_notice(this, notice, new PostDataTools.DataCallback() {

            @Override
            public void callback(boolean flag, String message, Object object) {

                CommandTools.hidenKeyboars(StoreNoticeActivity.this, tvNumber);
                finish();
            }
        });
    }

    public void onStop(){
        super.onStop();

    }
}
